#========================================
#    author: Changlong.Zang
#      mail: zclongpop123@163.com
#      time: Tue Apr 18 10:49:24 2017
#========================================
try:
    from PyQt5 import QtCore, QtWidgets
    import stQt5 as stQt

except ImportError:
    from PyQt4 import QtCore, QtGui
    import stQt4 as stQt
    QtWidgets = QtGui

import maya.cmds as mc
import stUtils, stModel, stCore
#--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
class ShotTableUI(QtWidgets.QMainWindow, stQt.Ui_shot_table_wnd):
    '''
    '''
    def __init__(self, parent=stUtils.get_maya_window()):
        '''
        '''
        if stUtils.windows_exists('shot_table_wnd'):
            mc.dockControl('Shot_Table', e=True, vis=True)
            return

        super(ShotTableUI, self).__init__(parent)
        self.setupUi(self)
        #-
        self.custom_initialize()
        #-
        self.docker = mc.dockControl('Shot Table', area='right', content=str(self.objectName()), allowedArea=('left', 'right'), vis=True)





    def custom_initialize(self):
        '''
        '''
        self.__table_model = stModel.TableModel()
        self.tableView.setModel(self.__table_model)
        self.tableView.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
        self.tableView.pressed.connect(self.tableview_press)





    def on_btn_refreshKeys_clicked(self, click=None):
        '''
        '''
        if click == None:
            return
        self.__table_model.updateData(stCore.get_reference_keys())





    def on_btn_bakekeys_clicked(self, click=None):
        '''
        '''
        if click == None:
            return

        namespaces = list()
        for index in self.tableView.selectedIndexes():
            ns = self.__table_model.headerData(index.column(), QtCore.Qt.Horizontal).rsplit('_', 1)[0]
            if not ns in namespaces:
                namespaces.append(ns)

        for ns in namespaces:
            stCore.bake_char_anim(ns)





    def on_btn_goto1p1_clicked(self, click=None):
        '''
        '''
        if click == None:
            return
        self.call_key_to_func(1)





    def on_btn_goto1p2_clicked(self, click=None):
        '''
        '''
        if click == None:
            return
        self.call_key_to_func(2)





    def on_btn_goto1p3_clicked(self, click=None):
        '''
        '''
        if click == None:
            return
        self.call_key_to_func(3)





    def call_key_to_func(self, step=1):
        '''
        '''
        namspaces = None
        keys = list()

        for index in self.tableView.selectedIndexes():
            ns = self.__table_model.headerData(index.column(), QtCore.Qt.Horizontal).rsplit('_', 1)[0]
            if not namspaces:
                namspaces = ns

            if ns == namspaces:
                keys.append(index.row())

        keys = [k for k in keys if k is not None]
        if len(keys) < 2:
            stCore.key_to_step(namspaces, step)

        else:
            start = min(keys)
            end   = max(keys)
            stCore.key_to_step(namspaces, step, start, end)





    def on_btn_cut_clicked(self, click=None):
        '''
        '''
        if click == None:
            return
        namespace, indexs = self.__get_select_items()
        if not indexs:
            return
        self.__copy_cut_key = namespace, indexs, 'Cut'
        self.btn_paste.setEnabled(True)





    def on_btn_copy_clicked(self, click=None):
        '''
        '''
        if click == None:
            return
        namespace, indexs = self.__get_select_items()
        if not indexs:
            return
        self.__copy_cut_key = namespace, indexs, 'Copy'
        self.btn_paste.setEnabled(True)





    def __get_select_items(self):
        '''
        '''
        namespace = None
        indexs    = list()
        for index in self.tableView.selectedIndexes():
            namsps = self.__table_model.headerData(index.column(), QtCore.Qt.Horizontal)
            if namespace is None:
                namespace = namsps

            if namsps == namespace:
                indexs.append(index.row())

        return namespace, indexs





    def on_btn_paste_clicked(self, click=None):
        '''
        '''
        if click == None:
            return

        paste_key = self.__get_select_items()
        stCore.paste_keys(self.__copy_cut_key, paste_key)
        self.btn_paste.setEnabled(False)





    def on_btn_delete_clicked(self, click=None):
        '''
        '''
        if click == None:
            return

        namespace, indexs = self.__get_select_items()
        stCore.delete_keys(namespace, indexs)





    def on_btn_setOrigKey_clicked(self, click=None):
        '''
        '''
        if click == None:
            return

        indexs = self.tableView.selectedIndexes()
        if not indexs:
            return

        namespace = self.__table_model.headerData(indexs[0].column(), QtCore.Qt.Horizontal).rsplit('_', 1)[0]
        frame     = mc.playbackOptions(q=True, ast=True) + indexs[0].row()

        stCore.set_keys(namespace, frame, 'Orig')





    def on_btn_setAnimKey_clicked(self, click=None):
        '''
        '''
        if click == None:
            return

        indexs = self.tableView.selectedIndexes()
        if not indexs:
            return

        namespace = self.__table_model.headerData(indexs[0].column(), QtCore.Qt.Horizontal).rsplit('_', 1)[0]
        frame     = mc.playbackOptions(q=True, ast=True) + indexs[0].row()

        stCore.set_keys(namespace, frame, 'Anim')





    def tableview_press(self, index):
        '''
        '''
        self.__table_model._src_index = index
